using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml;
using System.IO;
using System.Collections;
using QnA;

/// <summary>
/// Summary description for CreateXML_User
/// </summary>
public partial class CreateXML_Expert : System.Web.UI.Page
{
    ProcessUser processUser;
    public CreateXML_Expert()
	{
		//
		// TODO: Add constructor logic here
		//
	}
    public string writeXML(DataView dvUser)
    {
        string fileName = "" ;
        XmlTextWriter writer;
        try
        {
           
            fileName = Server.MapPath("Data/user.xml");

            writer = new XmlTextWriter(fileName, System.Text.Encoding.UTF8);
            writer.Formatting = Formatting.Indented;
            writer.Indentation = 6;
            writer.WriteStartDocument();

            writer.WriteStartElement("UserInfo");

            for (int i = 0; i < dvUser.Count; i++)
            {
                writer.WriteStartElement("User");
                writer.WriteAttributeString("UserID", dvUser[i]["UserID"].ToString());
                writer.WriteAttributeString("UserName", dvUser[i]["UserName"].ToString());
                writer.WriteAttributeString("Description", dvUser[i]["BriefDescription"].ToString());
                writer.WriteAttributeString("ChatPrice", dvUser[i]["ChatPrice"].ToString());
                                
                if (dvUser[i]["Image"].ToString() == "")
                {
                    writer.WriteAttributeString("Image", "~/image/user/none.bmp");
                }
                else
                { 
                    writer.WriteAttributeString("Image", "~" + dvUser[i]["Image"].ToString());
                }
               
                int status = int.Parse(dvUser[i]["Status"].ToString());
                if (status == 0) writer.WriteAttributeString("Status", "Offline");
                else if (status == 1) writer.WriteAttributeString("Status", "Online");
                else if (status == 2) writer.WriteAttributeString("Status", "Busy");

                if (dvUser[i]["Rating"].ToString() == "")
                {
                    writer.WriteAttributeString("Comment", "0");
                    writer.WriteAttributeString("Rating", "~/image/rating/0.bmp");
                }
                else
                {
                    writer.WriteAttributeString("Comment", dvUser[i]["TotalComment"].ToString());
                    string rate = dvUser[i]["Rating"].ToString();
                    if(rate == "1")
                    {
                        writer.WriteAttributeString("Rating", "~/image/rating/1.bmp");
                    }
                    else if (rate == "2")
                    {
                        writer.WriteAttributeString("Rating", "~/image/rating/2.bmp");
                    }
                    else if (rate == "3")
                    {
                        writer.WriteAttributeString("Rating", "~/image/rating/3.bmp");
                    }
                    else if (rate == "4")
                    {
                        writer.WriteAttributeString("Rating", "~/image/rating/4.bmp");
                    }
                    else if (rate == "5")
                    {
                        writer.WriteAttributeString("Rating", "~/image/rating/5.bmp");
                    }
                   
                }
                writer.WriteEndElement();
            }
            writer.WriteEndElement();
            writer.Flush();
            writer.Close();
           
        }
        catch (Exception ex)
        {
            HttpContext.Current.Response.Redirect("UserThongbao.aspx?id=" + ex.ToString());
        } 
        return fileName;
    }
}
